<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: product_filter.php 445 2014-01-19 12:01:36Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 445 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-19 12:01:36 +0000 (Sun, 19 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include ('includes/application_top.php');

$module_smarty = new Smarty();

include(DIR_WS_MODULES.'product_filter.php');
require(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

if(isset($_GET['filter_id']))
	$brand = " AND p.brand_id = '".$_GET['filter_id']."' ";
else
	$brand = '';
	
switch ($_GET['multisort']) {
	case 'specialprice':
		$sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC';
		$field = ' INNER JOIN '.TABLE_SPECIALS.' s ';
		break;
	case 'new_asc':
		$sorting = ' ORDER BY p.products_date_added ASC';
		break;
	case 'new_desc':
		$sorting = ' ORDER BY p.products_date_added DESC';
		break;
	case 'name_asc':
		$sorting = ' ORDER BY pd.products_name ASC';
		break;
	case 'name_desc':
		$sorting = ' ORDER BY pd.products_name DESC';
		break;
	case 'price_asc':
		$sorting = ' ORDER BY p.products_price ASC';
		break;
	case 'price_desc':
		$sorting = ' ORDER BY p.products_price DESC';
		break;
	case 'brand_asc':
		$sorting = ' ORDER BY b.brand_name ASC';
		break;
	case 'brand_desc':
		$sorting = ' ORDER BY b.brand_name DESC';
		break;
	default:
		$sorting = ' ORDER BY p.products_price ASC';
		break;
}

$check = $db->db_query("SELECT specials_id FROM ".TABLE_SPECIALS);
if(defined('MODULE_SMALL_BUSINESS_STATUS') && MODULE_SMALL_BUSINESS_STATUS == 'true') {
	if($check->_numOfRows) {
		$filter_sql = "SELECT
							p.products_id,
							p.products_quantity,
							p.products_fsk18,
							pd.products_name,
							pd.products_short_description,
							p.products_image,
							p.products_price,
							p.products_vpe,
							p.products_vpe_status,
							p.products_vpe_value
						FROM
							".TABLE_SPECIALS." s,
							".TABLE_PRODUCTS." p,
											
							LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd 
								ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')
						WHERE 
							p.products_status = '1'
						AND
							(IF(s.specials_price_".$_SESSION['customers_status']['customers_status_id']." = p.products_price, s.specials_price_".$_SESSION['customers_status']['customers_status_id']." >= '".($minp-0.01)."', p.products_price >= '".($minp-0.01)."')
							AND 					 
								(p.products_price <= '".($maxp+0.01)."'))
						".$pd.$brand.$group_check_p.$fsk_lock.$sorting;

	} else {	
		$filter_sql = "SELECT 
		                        p.products_id, 
		                        p.products_quantity, 
		                        p.products_fsk18, 
		                        pd.products_name, 
		                        pd.products_short_description, 
		                        p.products_image, 
		                        p.products_price, 
		                        p.products_vpe, 
		                        p.products_vpe_status, 
		                        p.products_vpe_value, 
		                        p.products_tax_class_id 
		                   FROM 
		                        ".TABLE_PRODUCTS." p 
		                        LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd  
		                             ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')  
		                        ".$field." 
		                   WHERE  
		                        (p.products_price >= '".($minp-0.01)."' AND p.products_price <= '".($maxp+0.01)."') 
		                        ".$pd." 
		                   AND 
		                        p.products_status = '1' 
		                        ".$brand.$group_check_p.$fsk_lock.$sorting;
	}
	
} else {
	if($check->_numOfRows) {
		$filter_sql = "SELECT
							p.products_id,
							p.products_quantity,
							p.products_fsk18,
							pd.products_name,
							pd.products_short_description,
							p.products_image,
							p.products_price,
							p.products_vpe,
							p.products_vpe_status,
							p.products_vpe_value,
							p.products_tax_class_id
						FROM
							".TABLE_SPECIALS." s,
							".TABLE_PRODUCTS." p					
							LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd 
								ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')
							INNER JOIN ".TABLE_TAX_RATES." AS x
								ON x.tax_rates_id = p.products_tax_class_id 
						WHERE 
							p.products_status = '1'
						AND
							(IF((s.specials_price_".$_SESSION['customers_status']['customers_status_id']."+s.specials_price_".$_SESSION['customers_status']['customers_status_id']."/100*x.tax_rate) = (p.products_price+p.products_price/100*x.tax_rate),(s.specials_price_".$_SESSION['customers_status']['customers_status_id']."+s.specials_price_".$_SESSION['customers_status']['customers_status_id']."/100*x.tax_rate)>= '".($minp-0.01)."',(p.products_price+p.products_price/100*x.tax_rate) >= '".($minp-0.01)."')
							AND 					 
								((p.products_price+p.products_price/100*x.tax_rate) <= '".($maxp+0.01)."'))
						".$pd.$brand.$group_check_p.$fsk_lock.$sorting;

	} else {	
		$filter_sql = "SELECT 
		                        p.products_id, 
		                        p.products_quantity, 
		                        p.products_fsk18, 
		                        pd.products_name, 
		                        pd.products_short_description, 
		                        p.products_image, 
		                        p.products_price, 
		                        p.products_vpe, 
		                        p.products_vpe_status, 
		                        p.products_vpe_value, 
		                        p.products_tax_class_id 
		                   FROM 
		                        ".TABLE_PRODUCTS." p 
		                        LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd  
		                             ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."') 
		                        INNER JOIN ".TABLE_TAX_RATES." AS x 
		                             ON x.tax_rates_id = p.products_tax_class_id  
		                        ".$field." 
		                   WHERE  
		                        ((p.products_price+p.products_price/100*x.tax_rate) >= '".($minp-0.01)."' AND (p.products_price+p.products_price/100*x.tax_rate) <= '".($maxp+0.01)."') 
		                        ".$pd." 
		                   AND 
		                        p.products_status = '1' 
		                        ".$brand.$group_check_p.$fsk_lock.$sorting;
	}
}

$listing = new page_break($filter_sql, $_GET['page'], true, 'product_filter.php'.$filter_param, 'product_filter');
if($listing->result) {
	$module_content = array();
	$rows = 0;
	while(!$listing->sql_string->EOF) {	$rows ++;
		if($listing->listName == 'mosaic' && empty($listing->sql_string->fields['products_image'])) {}
		else $module_content[] = $product->buildDataArray($listing->sql_string->fields, 'thumbnail', $listing->listData, $rows);
		$p_ids[] = $listing->sql_string->fields['products_id'];
		$listing->sql_string->MoveNext();
	}
	
	$module_smarty->assign('language', $_SESSION['language']);
	
	if(isset($_GET['filter_id'])) {
		$getBrand = $db->db_query("SELECT brand_name FROM ".TABLE_BRAND." WHERE brand_id = '".$_GET['filter_id']."' ");
		$brands = $getBrand->fields['brand_name'];
	}
	
	$module_smarty->assign('NAVIGATION', $listing->getLinks());
	$title = !empty($filter_names) ? 'Suchparameter: ' . $filter_names : '';
	
	if(!empty($brands) || !empty($sort))
		$title .= '<br />Suchfilter: '.$brands.(empty($brands)?'':', ').$sort;
	
	if(isset($_GET['multisort'])) {
		$sorts = array('new_desc' => MULTISORT_NEW_DESC,
						'new_asc' => MULTISORT_NEW_ASC,
						'price_asc' => MULTISORT_PRICE_ASC,
						'price_desc' => MULTISORT_PRICE_DESC,
						'name_asc' => MULTISORT_ABC_AZ,
						'name_desc' => MULTISORT_ABC_ZA);
						
		$sort = $sorts[$_GET['multisort']];
	}

	$module_smarty->assign('TITLE', $title);
	$module_smarty->assign('LIST_NAME', $listing->page_name);
	$module_smarty->assign('CLASS_NAME', $listing->listName);
	$module_smarty->assign('module_content', $module_content);
	
	include(DIR_WS_MODULES.'page_view_as.php');
	include(DIR_WS_MODULES.'page_per_site.php');
	include(DIR_WS_MODULES.'page_mulit_sort.php');
	include(DIR_WS_MODULES.'page_brand.php');
	
	$module = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/'.$listing->listData['list_file'].'.html');
	$smarty->assign('main_content', $module);
	
} else {
	$error = 'Zu den Suchparametern <b>'.$price->format($minp, true).'-'.$price->format($maxp, true).(!empty($filter_names) ? ', '.$filter_names : '').$brands.'</b> wurden keine passenden Produkte gefunden.';
	include(DIR_WS_MODULES.FILENAME_ERROR_HANDLER);
}
require_once(DIR_WS_INCLUDES.'xajax/xajax_core/xajax.inc.php');
include(DIR_WS_INCLUDES.'xajax/xajax.add_cart.php');
require(DIR_WS_INCLUDES.'header.php');

$smarty->assign('language', $_SESSION['language']);
$smarty->caching = false;
if (!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');